//
//  XNoteBookDetailViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/7.
//

#import "XNoteBookDetailViewController.h"
#import "XNoteBookEditViewController.h"

@interface XNoteBookDetailViewController ()


@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 名称
@property(nonatomic,strong)UILabel* titleLb;

/// 占位文本
@property(nonatomic,strong)UILabel* updateLb;

/// 数量
@property(nonatomic,strong)UILabel* contentLb;


@property(nonatomic,strong)MyLinearLayout *bottomLayout;

/// x
@property(nonatomic,strong)UIButton* prevBtn;

/// x
@property(nonatomic,strong)UIButton* nextBtn;

@end

@implementation XNoteBookDetailViewController


- (void)loadView{
    [super loadView];
    // 线性布局
    self.view.backgroundColor = COLOR_WHITE;
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myMargin = 0;
    [self.view addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    //底部
    MyLinearLayout *bottomLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    bottomLayout.myHeight = PX(100);
    bottomLayout.myHorzMargin = 0;
    bottomLayout.myBottom = IPHONE_BOTTOM_SAFEINSET;
    bottomLayout.padding = UIEdgeInsetsMake(0, IPHONE_BOTTOM_SAFEINSET, 0, 0);
    bottomLayout.gravity = MyGravity_Horz_Fill;
    [self.view addSubview:bottomLayout];
    _bottomLayout = bottomLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    [self updateViews];
}

- (void)setupNavBar{
    
    self.navigationItem.title = @"查看笔记";

    UIBarButtonItem* editItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(editClicked:)];
    
    UIBarButtonItem* delItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:(UIBarButtonItemStylePlain) target:self action:@selector(delClicked:)];
    
    self.navigationItem.rightBarButtonItems = @[editItem,delItem];
}

- (void)setupViews{
    
    UILabel* titleLb = [UILabel lableWithFont:FONTMID_FIT(17) color:COLOR_BLACK_DARK text:NULL];
    titleLb.numberOfLines = 2;
    titleLb.myHorzMargin = 20;
    titleLb.myTop = 20;
    titleLb.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UILabel* contentLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_BASE text:NULL];
    contentLb.myHorzMargin = 20;
    contentLb.myTop = 16;
    contentLb.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:contentLb];
    _contentLb = contentLb;
    
    UILabel* updateLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_BASE text:NULL];
    updateLb.myHorzMargin = 20;
    updateLb.myTop = 24;
    updateLb.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:updateLb];
    _updateLb = updateLb;
    
    
    [self addBottomBtn:@"上一个" sel:@selector(preClicked:)];
    [self addBottomBtn:@"下一个" sel:@selector(nextClicked:)];
}

- (void)addBottomBtn:(NSString*)imageName sel:(SEL)sel{
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    btn.myMargin = 0;
    btn.mySize = CGSizeMake(PX(60), PX(60));
    [self.bottomLayout addSubview:btn];
}

- (void)updateViews{
    
    self.titleLb.text = self.model.title;
    [self.titleLb sizeToFit];
    
    self.updateLb.text = [self.model.update stringWithFormat:@"yyyy.MM.dd "];
    [self.updateLb sizeToFit];
    
    self.contentLb.text = self.model.content;
    [self.contentLb sizeToFit];
}

#pragma mark  --- action
/// 编辑
- (void)editClicked:(id)sender{
    
    XNoteBookEditViewController* editController = [XNoteBookEditViewController new];
    editController.operation = EditOperation_Edit;
    editController.model = self.model;
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        
        self.model = obj;
        
        [self updateViews];
    };
    [UIViewController showViewController:editController animated:YES];
}

/// 删除
- (void)delClicked:(id)sender{
    
    RLMResults* ret = [XNoteBook objectsWhere:@"id = %@",self.model.id];
    [[RLMRealm defaultRealm] beginWriteTransaction];
    [[RLMRealm defaultRealm] deleteObjects:ret];
    [[RLMRealm defaultRealm] commitWriteTransaction];

    [self closeViewControllerAnimated:YES];
}

/// 上一个
- (void)preClicked:(UIButton*)sender{
    

    [self updateViews];
}

/// 下一个
- (void)nextClicked:(UIButton*)sender{
    
    [self updateViews];
}
@end
